/**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 */
define(['N/record', 'N/render', 'N/file', 'N/search'], function (record, render, file, search) {
    function beforeLoad(context) {
        if (context.type === 'view') {
            try {
                var rec = record.load({
                    type: record.Type.ITEM_RECEIPT,
                    id: context.newRecord.id,
                    isDynamic: true
                });

                var createdfrom = rec.getText('createdfrom');
                var createdfromId = rec.getValue('createdfrom');
                var ir = {};
                ir.memo = rec.getText('memo');
                // var subsidiaryRec = record.load({
                //     type: 'subsidiary',
                //     id: rec.getValue('subsidiary')
                // });
                //ir.currency = subsidiaryRec.getText('currency');
                ir.printTimes = rec.getValue('custbody_print_times');
                var createdfromRec;
                if (createdfrom.indexOf("采购订单") !== -1 || createdfrom.indexOf("Purchase Order") !== -1) {
                    createdfromRec = record.load({
                        type: record.Type.PURCHASE_ORDER,
                        id: createdfromId,
                        isDynamic: true
                    });
                    ir.vendor = createdfromRec.getText('entity');
                    ir.contractno = createdfromRec.getText('custbody_contractno');
                } else if (createdfrom.indexOf("调拨单") !== -1 || createdfrom.indexOf("Transfer Order") !== -1) {
                    createdfromRec = record.load({
                        type: record.Type.TRANSFER_ORDER,
                        id: createdfromId,
                        isDynamic: true
                    });
                    ir.contractno = createdfromRec.getText('tranid');

                } else {
                    return;
                }
                ir.subsidiary = rec.getText('subsidiary');
                ir.trandate = rec.getText('trandate');
                ir.tranid = rec.getValue('tranid');


                ir.truckno = rec.getText('custbody_truckno');
                ir.totalreceiptnumber = rec.getText('custbody_totalreceiptnumber');
                ir.differencenumber = rec.getText('custbody_differencenumber');
                ir.differentproduct = rec.getText('custbody_differentproduct');

                var items = [];
                var lineCount = rec.getLineCount('item');
                var totalQuantity = 0;
                var totalAllQuantity = 0;
                for (var i = 0; i < lineCount; i++) {
                    var item = {};
                    rec.selectLine('item', i);
                    item.lineNumber = i + 1;

                    var itemId = rec.getCurrentSublistValue('item', 'item');
                    var itemRec;
                    try {
                        itemRec = record.load({
                            type: record.Type.ASSEMBLY_ITEM,
                            id: itemId,
                            isDynamic: true
                        });
                        item.size = itemRec.getText('custitem_size');
                    } catch (e) {
                        itemRec = record.load({
                            type: record.Type.INVENTORY_ITEM,
                            id: itemId,
                            isDynamic: true
                        });
                        item.size = itemRec.getText('custitem_door_width');
                    }
                    item.sku = itemRec.getText('custitem_child_sku');
                    item.name = itemRec.getText('displayname');
                    item.code = itemRec.getText('itemid');
                    item.color = itemRec.getText('custitem_color');
                    item.colorNumber = itemRec.getText('custitem_color_number');
                    item.flowerType = itemRec.getText('custitem_flower_type');
                    item.flowerTypeColor = itemRec.getText('custitem_flower_type_color');


                    item.quantity = Number(rec.getCurrentSublistValue('item', 'quantity'));

                    var itemLine = createdfromRec.findSublistLineWithValue('item', 'item', itemId);
                    item.allQuantity = Number(createdfromRec.getSublistValue('item', 'quantity', itemLine));


                    totalQuantity += item.quantity;
                    totalAllQuantity += item.allQuantity;
                    item.unit = itemRec.getText('stockunit');

                    item.remarks = rec.getCurrentSublistText('item', 'custcol_remarks');

                    items.push(item);
                }
                ir.items = items;
                ir.totalQuantity = totalQuantity;
                ir.totalAllQuantity = totalAllQuantity;
                log.debug('ir', ir);

                //调用模板文件
                var renderer = render.create();
                renderer.templateContent = file.load(1368).getContents();
                renderer.addCustomDataSource({
                    format: render.DataSource.JSON,
                    alias: 'ir',
                    data: JSON.stringify(ir)
                });
                var pdfFile = renderer.renderAsPdf();
                //PDF文件存放文件夹ID
                pdfFile.folder = 185;
                pdfFile.name = context.newRecord.id + new Date().getTime() + '.pdf';
                var fileId = pdfFile.save();
                var invalidFile = record.create({type: 'customrecord_invalid_file_id'});
                invalidFile.setValue('custrecord_invalid_file_id', fileId);
                invalidFile.save();
                var File = file.load(fileId);
                var pdfurl = File.url;
                var form = context.form;
                form.addButton({
                    id: 'custpage_button',
                    label: '打印收货单',
                    functionName: "window.open('" + pdfurl + "');"
                });
            } catch (e) {
                log.error(e.name, JSON.stringify(e));
            }

        }
    }


    return {
        beforeLoad: beforeLoad
    };
});
